---
title: Loops and Iteration
description: Learn how to add multiple messages based on loop conditions in your prompts
---

## Overview

Loops in PromptL allow you to dynamically generate content or messages by iterating over lists or arrays. This is particularly useful for creating adaptive prompts based on user input or contextual data.

With loops, you can:
- Repeat sections of your prompt for each item in a list.
- Access each item's index for numbered output or additional logic.
- Handle empty lists gracefully using the `else` clause.

---

## Syntax

### Basic Loop
A loop is defined using the `for` and `endfor` keywords, wrapped in `{{ }}`. The content inside the loop is repeated for each item in the list.

```tsx
{{ for item in list }}
  - {{ item }}
{{ endfor }}
```

### Loop with Index
You can include an `index` parameter to track the iteration count, starting from `0`.

```tsx
{{ for item, index in list }}
  {{ index }}: {{ item }}
{{ endfor }}
```

### Loop with `else`
The `else` clause runs when the list is empty. Place it before the `endfor` keyword.

```tsx
{{ for item in list }}
  - {{ item }}
{{ else }}
  No items to display.
{{ endfor }}
```

---

## Examples

### Basic Example: Listing Items
```tsx
{{ for fruit in ["apple", "banana", "cherry"] }}
  - {{ fruit }}
{{ endfor }}
```

**Output**:
```
- apple
- banana
- cherry
```

### Example with Index
```tsx
{{ for fruit, index in ["apple", "banana", "cherry"] }}
  {{ index + 1 }}. {{ fruit }}
{{ endfor }}
```

**Output**:
```
1. apple
2. banana
3. cherry
```

### Handling Empty Lists
```tsx
{{ for item in [] }}
  - {{ item }}
{{ else }}
  The list is empty. No items to display.
{{ endfor }}
```

**Output**:
```
The list is empty. No items to display.
```

---

## Advanced Usage

### Iterating Over Objects
Loops can handle more complex data structures, such as arrays of objects.

```tsx
{{ for user in users }}
  - Name: {{ user.name }}, Age: {{ user.age }}
{{ endfor }}
```

For the input:
```tsx
users = [
  { name: "Alice", age: 30 },
  { name: "Bob", age: 25 }
]
```

**Output**:
```
- Name: Alice, Age: 30
- Name: Bob, Age: 25
```

---

### Nested Loops
You can use nested loops for iterating over multi-dimensional data.

```tsx
{{ for group, groupIndex in groups }}
  Group {{ groupIndex + 1 }}:
  {{ for member in group.members }}
    - {{ member }}
  {{ endfor }}
{{ endfor }}
```

For the input:
```tsx
groups = [
  { members: ["Alice", "Bob"] },
  { members: ["Charlie", "Dana"] }
]
```

**Output**:
```
Group 1:
  - Alice
  - Bob
Group 2:
  - Charlie
  - Dana
```

---

## Best Practices

1. **Keep Loops Simple**:
   - Avoid deeply nested loops unless necessary. Complex loops can make your prompts harder to read and maintain.
2. **Use Default Values**:
   - Provide defaults for variables to prevent errors when lists are empty or data is incomplete.
   - Example: `{{ item || "Unknown" }}`
3. **Combine with Conditionals**:
   - Use `if` statements inside loops for conditional logic.
   ```tsx
   {{ for user in users }}
     {{ if user.active }}
       - {{ user.name }} (Active)
     {{ else }}
       - {{ user.name }} (Inactive)
     {{ endif }}
   {{ endfor }}
   ```
4. **Debugging**:
   - Temporarily output the list and its elements to ensure the loop is iterating as expected.

---

## Debugging Tips

If your loop isn’t working as expected:
- **Verify Data**: Print the list you’re iterating over to ensure it contains the expected data.
- **Check Syntax**: Ensure `endfor` is present and properly matched with `for`.
- **Use `else` for Debugging**: Add an `else` clause to confirm whether the list is empty.

```tsx
{{ for item in list }}
  - {{ item }}
{{ else }}
  Debug: The list is empty or not defined.
{{ endfor }}
```

---

## Summary

Loops in PromptL enable you to iterate over lists dynamically, generate repeated content, and handle complex data structures. By combining loops with variables and conditionals, you can create powerful, adaptive prompts tailored to any use case.

> Ready for more dynamic control? Explore [Conditional Statements](/promptl/syntax/conditionals) to complement your loops.
